package com.cssl.message;

import com.cssl.service.ProductService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MessageConsumer {

    @Autowired
    private ProductService ps; //操作商品表

    /**
     * 扣减库存
     * @param map
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "q11"),
            exchange = @Exchange(value = "inventory",type = ExchangeTypes.DIRECT),
            key = "kc"
    ))
    public void reduceInventory(Map<Integer,Integer> map) {
        System.out.println("消费者收到消息reduceInventory："+map);
        if(ps.reduceInventory(map))
            System.out.println("扣减库存成功！");
        else
            System.out.println("扣减库存失败！");
    }

    /**
     * 回滚扣减库存
     * @param map
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "q66"),
            exchange = @Exchange(value = "inventory",type = ExchangeTypes.DIRECT),
            key = "hgkc"
    ))
    public void HgReduceInventory(Map<Integer,Integer> map) {
        System.out.println("消费者收到消息reduceInventory："+map);
        if(ps.HgReduceInventory(map))
            System.out.println("回滚扣减库存成功！");
        else
            System.out.println("回滚扣减库存失败！");
    }


    /**
     * 直接接收延时消息
     */
    //@RabbitListener(queues = {"delayQueue"})
    public void receiveDelay(String msg){
        System.out.println("delayQueue:"+msg);
    }

}
